//
//  BindEmailViewController.m
//  reSearchDemo
//
//  Created by helfy  on 15-4-17.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "BindEmailViewController.h"

@interface BindEmailViewController ()
{
    UITextField *textFiled;
}
@end

@implementation BindEmailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的邮箱";
    
    textFiled = [UITextField new];
    [self.view addSubview:textFiled];
    textFiled.borderStyle = UITextBorderStyleLine;
    textFiled.placeholder = @"请输入邮箱地址";
    
    [textFiled mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(100);
        make.left.equalTo(10);
        make.right.equalTo(-10);
        make.height.equalTo(45);
    }];
   
    UIButton *saveButton = [UIButton new];
    [self.view addSubview:saveButton];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(setEmial) forControlEvents:UIControlEventTouchUpInside];
    saveButton.backgroundColor = [UIColor grayColor];
    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textFiled.mas_bottom).equalTo(40);
        make.left.equalTo(10);
        make.right.equalTo(-10);
        make.height.equalTo(45);
    }];
    
    [self getEmail];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getEmail
{
    if ([super startRequest]) {
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        [dic setObject:[BSEngine currentEngine].user.uid forKey:@"uid"];
        [client requestFor:dic methodName:@"User/Api/getEmail"];
    }
    


}

-(BOOL)isValidateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}


-(void)setEmial
{
    if(![self isValidateEmail:textFiled.text])
    {
        [self showText:@"请输入正确的邮箱地址"];
        return;
    }
    if ([super startRequest]) {
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        [dic setObject:[BSEngine currentEngine].user.uid forKey:@"uid"];
        [dic setObject:textFiled.text forKey:@"email"];
        [client requestFor:dic methodName:@"User/Api/setEmail"];
    }
    
}
- (BOOL)requestDidFinish:(BSClient *)sender obj:(NSDictionary *)obj {
    if ([super requestDidFinish:sender obj:obj]) {
        
        if([obj[@"state"][@"url"] isEqualToString:@"User/Api/getEmail"])
        {
              NSString  *email = obj[@"data"][@"email"];
            if(email.length>0)
            {
                textFiled.text = email;
            }
        }
        else {
        NSDictionary *data = obj[@"data"];
        if([data isKindOfClass:[NSDictionary class]] && sender.hasError)
        {
            NSString *msg = data[@"msg"];
            [self showText:msg];
        }
        else{
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        }
        
    }
    return NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
